import { getInitApi } from '@/api/login'
import { getConfigApi } from '@/api/system/config'
import useConfigStore from '@/store/modules/config'

export function useConfig(_key) {
  const result = ref('')
  if (!_key) return result
  const data = useConfigStore().getDict(_key)
  if (data === null) {
    getConfigApi(_key).then(res => {
      result.value = res?.value || ''
      useConfigStore().setConfig(_key, result.value)
    })
  } else {
    result.value = data
  }
  return result
}

export function useInitLoad(_key) {
  const result = ref('')
  if (!_key) return result
  let data = useConfigStore().dataLoad()
  if (data === null) {
    getInitApi().then(res => {
      data = res?.config || null
      useConfigStore().setLoad(data)
      result.value = data && _key in data ? data[_key] : ''
    })
  } else {
    result.value = _key in data ? data[_key] : ''
  }
  return result
}
